package com.zrrd.learn.chapter04;

import java.util.Arrays;

/**
 * 学习一下调整为数组
 *
 * @author wrn
 * @version 1.0
 * @date 2023/5/20 13:26
 */

public class LearnArray {

    public static void main(String[] args) {

//        [类型][][标识符][=][new][长度]
//        char[] c1= new char[3];
//        System.out.println(Arrays.toString(c1));
//        String a = null;
//        System.out.println(a);
//        int[] int1 = new int[4];
//        int1[0] = 2;
//      默认值
//        {1}{2}{3}{4}{5}{6}
//        int[] int2 = {1,2,3,4,5,6};
//        System.out.println(Arrays.toString(int2));
//        for (int i = 1; i < int2.length; i++) {
//            int2[i] = int2[i] + int2[i-1];
//        }
//        System.out.println(Arrays.toString(int2));
//
//        for (int i = int2.length-1; i > 0 ; i--) {
//            int2[i] = int2[i] + int2[i-1];
//        }
//        System.out.println(Arrays.toString(int2));

//加法
//        int c = 9;
//        int b = 13;
//        b = b + c;
//        c = b - c;
//        b = b - c;
//        System.out.println(c);
//        System.out.println(b);
//        //减法
//        int e = 9;
//        int f = 13;
//        f = f - e;
//        e = f + e;
//        f = e - f;
//        System.out.println(e);
//        System.out.println(f);
//        //乘法
//        int c2 = 9;
//        int b2 = 13;
//        b2 = b2 * c2;
//        c2 = b2 / c2;
//        b2 = b2 / c2;
//        System.out.println(c2);
//        System.out.println(b2);
//        //^
//        int d = 9;
//        int s = 13;
//        d = d ^ s;
//        s = d ^ s;
//        d = d ^ s;
//        System.out.println(d);
//        System.out.println(s);
        int[] arr2 = new int[]{2, 5, 1};

        bubble(arr2);
        System.out.println(Arrays.toString(arr2));
    }

    /**
     * 冒泡排序
     * @param arr2
     */
    public static void bubble(int[] arr2) {
        for (int j = 0; j < arr2.length - 1; j++) {
            for (int i = 0; i < arr2.length - 1 - j; i++) {
                if (arr2[i] > arr2[i + 1]) {
                    arr2[i] = arr2[i] + arr2[i + 1];
                    arr2[i + 1] = arr2[i] - arr2[i + 1];
                    arr2[i] = arr2[i] - arr2[i + 1];
                }
            }
        }
    }

    /**
     * 选择排序
     * @param arr2
     */
    public static void choose(int[] arr2) {
        for (int j = 0; j < arr2.length - 1; j++) {


            int maxVal = Integer.MIN_VALUE;
            int index = 0;
            for (int i = 0; i < arr2.length - j; i++) {
                if (maxVal < arr2[i]) {
                    maxVal = arr2[i];
                    index = i;
                }
            }
            int temp = arr2[index];
            arr2[index] = arr2[arr2.length - 1 - j];
            arr2[arr2.length - 1 - j] = temp;
        }
        System.out.println(Arrays.toString(arr2));
        //排序算法
    }

}


